#coding:utf-8
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
import time
# Create your views here.
def index(request):
    return HttpResponse("index")

def login(request):
    if request.method=='POST':
        username=request.POST.get('username')
        pwd= request.POST.get('pwd')
        if username == 'user1' and pwd == '123':
            request.session['is_login'] = True
            request.session['username'] = 'user1'
            return redirect('/order/')
    return render(request,'login.html')

# def home(request):
#     is_login = request.session.get('IS_LOGIN',False)    #False是默认值，不设置会报错
#     if is_login:
#         return HttpResponse('order')
#     else:
#         return redirect("/app01/login/")

def page(request,v1,v2):
    return HttpResponse(v1+v2)

from django.views.decorators.cache import cache_page

@cache_page(60 * 15)    #15分钟
def cache(request):
    current = str(time.time())
    return HttpResponse(current)

def order(request):
    is_login = request.session.get('is_login',False)    #False是默认值，不设置会报错
    if is_login:
        username = request.session.get('username',False)
        return render(request,'order.html',{'username':username})
    else:
        return redirect('/login/')

def logout(request):
    del request.session['is_login']
    return redirect('/login/')